package 实验四1;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class PriceAnalyzer {
    public static void main(String[] args) {
    	System.out.println("程序开始执行");
    	File file = new File("C:/Users/27991/Desktop/java/prices.txt.txt");
        Scanner sc = null;
        int count = 0;
        double sum = 0;
        try {
            sc = new Scanner(file);
            sc.useDelimiter("元/台");
            while (sc.hasNextDouble()) {
                double price = sc.nextDouble();
                count++;
                sum += price;
            }
            if (count > 0) {
                double averagePrice = sum / count;
                try (FileWriter writer = new FileWriter(file, true)) {
                    writer.write("\n平均价格：" + averagePrice + "元/台");
                    System.out.println("平均价格计算完成：" + averagePrice);
                    System.out.println("已经成功写入" );
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("文件中没有有效的价格数据。");
            }
        } catch (Exception exp) {
            exp.printStackTrace();
        } finally {
            if (sc!= null) {
                sc.close();
            }
        }
	}

}
